package net.i4q.sqlved.components;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class NewConnectionDialog extends JDialog implements ActionListener {
	private final Font headerFont = new Font(Font.SANS_SERIF,Font.BOLD,16);
	private static final int DIALOG_WIDTH = 300;
	private static final int DIALOG_HEIGHT = 200;
	private static final int LABEL_WIDTH = 90;
	private static final int TEXTFIELD_WIDTH = 12;
	
	private JLabel headerLabel = new JLabel("New Connection");
	private JLabel hostnameLabel = new JLabel("Hostname");
	private JTextField hostnameField = new JTextField(TEXTFIELD_WIDTH);
	private JLabel portLabel = new JLabel("Port");
	private JTextField portField = new JTextField("3306",TEXTFIELD_WIDTH);
	private JLabel userLabel = new JLabel("User");
	private JTextField userField = new JTextField(TEXTFIELD_WIDTH);
	private JLabel passwordLabel = new JLabel("Password");
	private JPasswordField passwordField = new JPasswordField(TEXTFIELD_WIDTH);
	private JButton connectButton = new JButton("Connect");
	private JButton cancelButton = new JButton("Cancel");
	
	NewConnectionDialog(JFrame frame, boolean modal) {
		super(frame,modal);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setSize(DIALOG_WIDTH,DIALOG_HEIGHT);
		this.setResizable(false);
		this.center();
		this.setLayout(new FlowLayout());
		this.initComponents();
	}
	
	private void initComponents() {
		headerLabel.setFont(headerFont);
		headerLabel.setPreferredSize(new Dimension(DIALOG_HEIGHT,headerLabel.getPreferredSize().height+20));
		hostnameLabel.setPreferredSize(new Dimension(LABEL_WIDTH,hostnameLabel.getPreferredSize().height));
		portLabel.setPreferredSize(new Dimension(LABEL_WIDTH, portLabel.getPreferredSize().height));
		userLabel.setPreferredSize(new Dimension(LABEL_WIDTH, userLabel.getPreferredSize().height));
		passwordLabel.setPreferredSize(new Dimension(LABEL_WIDTH, passwordLabel.getPreferredSize().height));
		connectButton.setActionCommand("connect");
		cancelButton.setActionCommand("cancelNewConnectionDialog");
		
		this.add(headerLabel);
		headerLabel.setHorizontalAlignment(JLabel.CENTER);
		this.add(hostnameLabel);
		this.add(hostnameField);
		this.add(portLabel);
		this.add(portField);
		this.add(userLabel);
		this.add(userField);
		this.add(passwordLabel);
		this.add(passwordField);
		this.add(connectButton);
		this.add(cancelButton);
	}
	
	private void center() {
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension screenDimension = toolkit.getScreenSize();
		int width = this.getWidth();
		int height = this.getHeight();
		int x = (int) ((screenDimension.getWidth() - width) / 2);
		int y = (int) ((screenDimension.getHeight() - height) / 2);
		this.setBounds(x, y, width, height);
	}
	
	public void addActionListener(ActionListener al) {
		connectButton.addActionListener(al);
		cancelButton.addActionListener(al);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		NewConnectionDialog dialog = new NewConnectionDialog(null,true);
		dialog.addActionListener(dialog);
		dialog.setVisible(true);
	}
	
	public Properties getParameters() {
		Properties props = new Properties();
		props.put("hostname", hostnameField.getText());
		props.put("port", portField.getText());
		props.put("user", userField.getText());
		props.put("password", new String(passwordField.getPassword()));
		
		return props;
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		String actionCommand = ae.getActionCommand();
		if(actionCommand.equals("cancel")) {
			this.dispose();
		} else if(actionCommand.equals("connect")) {
			
		}
	}

}
